package com.ebelter.btcomlib.models.bluetooth.products.scale;


import android.text.TextUtils;

import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.bluetooth.bean.SendMessage;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.Calendar;


/**
 * byte制包工具类
 */
public class ScaleBytesMakeFatory {

    private static final String TAG = "ScaleBytesMakeFatory";

    //心跳包字节
    private static final byte[] HEARTCHECKBYTES = {(byte) 0xab, 0x01, (byte) 0xb0};
    private static final String HEARTCHECK = "心跳包";

    public static SendMessage getHEARTSmg() {
        SendMessage heartMsg = new SendMessage();
        heartMsg.datas = HEARTCHECKBYTES;
        heartMsg.desc = HEARTCHECK;
        heartMsg.isNeedEncrypt = true;
        return heartMsg;
    }


    private static final String GET_HISTORY_DATA = "获取历史数据";

    public SendMessage getHistoryDataSmg(String uid, String connectName) {
        SendMessage msg = new SendMessage();
        msg.datas = getHistoryData(uid, connectName);
        msg.desc = GET_HISTORY_DATA;
        msg.isNeedEncrypt = true;

        LogUtils.i(TAG, "------获取历史数据的下发包 connectName = " + connectName + "---datas = " + BytesUtils.bytes2HexStr(msg.datas));
        return msg;
    }

    //获取历史数据
    private byte[] getHistoryData(String uid, String connectName) {
        String[] uidArr = uid.split(":");
        if (uidArr.length != 7) {
            uidArr = new String[]{"0", "0", "0", "0", "0", "0", "0"};
        }

        if (TextUtils.equals(ScaleConstant.SCALE_NAME, connectName)) {
            //旧版本
            byte[] bytes = {
                    (byte) 0xab,
                    0x07,
                    (byte) 0x9b,
                    (byte) Integer.parseInt(uidArr[0], 16),
                    (byte) Integer.parseInt(uidArr[1], 16),
                    (byte) Integer.parseInt(uidArr[2], 16),
                    (byte) Integer.parseInt(uidArr[3], 16),
                    (byte) Integer.parseInt(uidArr[4], 16),
                    (byte) Integer.parseInt(uidArr[5], 16),
                    (byte) Integer.parseInt(uidArr[6], 16),
                    0x00
            };

            return bytes;

        } else {
            //新版本
            byte[] bytes = {
                    (byte) 0xab,
                    0x08,
                    (byte) 0x9b,
                    (byte) Integer.parseInt(uidArr[0], 16),
                    (byte) Integer.parseInt(uidArr[1], 16),
                    (byte) Integer.parseInt(uidArr[2], 16),
                    (byte) Integer.parseInt(uidArr[3], 16),
                    (byte) Integer.parseInt(uidArr[4], 16),
                    (byte) Integer.parseInt(uidArr[5], 16),
                    (byte) Integer.parseInt(uidArr[6], 16),
                    0x00
            };
            return bytes;

        }


    }


    public SendMessage getSleepDisconnectTime(int time) {
        byte[] bytes = {
                (byte) 0xab,
                0x03,
                (byte) 0xa2, (byte) time, 0};

        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas = bytes;
        sendUserMsg.desc = "设置休眠断开时长";
        sendUserMsg.isNeedEncrypt = true;
        return sendUserMsg;
    }


    //接受到离线数据的一个回应ACK
    private static final byte[] getHistoryDataACK = {(byte) 0xab, 0x02, (byte) 0x9b, 0x01};

    //public byte[] getHistoryDataACK(){
//        return getHistoryDataACK;
//    }


    private static final String GET_HISTORY_DATA_ACK = "获取历史数据的回应";

    public SendMessage getetHistoryDataACK() {
        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas = getHistoryDataACK;
        sendUserMsg.desc = GET_HISTORY_DATA_ACK;
        sendUserMsg.isNeedEncrypt = true;
        return sendUserMsg;
    }

    private static final String GetMesureDataACK = "获取测量数据的回应";
    private static final byte[] getMesureDataACK = {(byte) 0xab, 0x02, (byte) 0xa3, 0x00};

    public SendMessage getMesureDataACK() {
        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas = getMesureDataACK;
        sendUserMsg.desc = GetMesureDataACK;
        sendUserMsg.isNeedEncrypt = true;
        return sendUserMsg;
    }

    private static final byte[] DeleteAllUserInfoByte = {(byte) 0xab, 0x02, (byte) 0xa1, 0x00};
    //    //清空用户信息的bytes
//    public byte[] getDeleteAllUserInfo(){
//        return DeleteAllUserInfoByte;
//    }
    private static final String DeleteAllUserInfo = "删除所有用户信息";

    public SendMessage deleteAllUserInfo() {
        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas = DeleteAllUserInfoByte;
        sendUserMsg.desc = DeleteAllUserInfo;
        sendUserMsg.isNeedEncrypt = true;
        return sendUserMsg;
    }


    //同步系统时间
    private byte[] getSystemClock() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int date = cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        //周一至周日，1至7
        int week = cal.get(Calendar.DAY_OF_WEEK);
        week = (week == 1) ? 7 : (week - 1);

        int yearLowHex = year & 0xff;
        int yearHeightHex = (year >> 8) & 0xff;
        LogUtils.i(TAG, "year=" + year + "--yearLowHex=" + yearLowHex + "--yearHeightHex=" + yearHeightHex + "--month=" + month
                + "--date=" + date + "--hour=" + hour + "--minute=" + minute + "--second=" + second + "--week=" + week);
        byte[] bytes = {
                (byte) 0xab,
                0x09,
                (byte) 0x98,
                (byte) yearLowHex,
                (byte) yearHeightHex,
                (byte) month,
                (byte) date,
                (byte) hour,
                (byte) minute,
                (byte) second,
                (byte) week
        };
        return bytes;
    }

    private static final String SystemClock = "下发系统时间";

    public SendMessage getSyncSystemClock() {
        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas = getSystemClock();
        sendUserMsg.desc = SystemClock;
        sendUserMsg.isNeedEncrypt = true;
        return sendUserMsg;
    }


    private static final String USERINFO = "下发用户信息";

    public SendMessage getUserInfoSmg(ScaleUser scaleUser) {
        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas = scaleUser.buildScaleUserData();
        sendUserMsg.desc = USERINFO;
        sendUserMsg.isNeedEncrypt = true;
        return sendUserMsg;
    }


    //OTA请求升级CRC
    private static final String OTAUPDATEREQUEST = "OTA升级请求";

    /**
     * @param type 1蓝牙固件 2秤固件
     */
    public SendMessage getOTAUpdateRequest(int type, int pkgLen, byte cs) {
        int lenLowHex = pkgLen & 0xff;
        int lenHeightHex = (pkgLen >> 8) & 0xff;

        byte[] bytes = {
                (byte) 0xab,
                0x06,
                (byte) 0x9d,
                (byte) type,
                (byte) lenLowHex,
                (byte) lenHeightHex,
                cs,
                0
        };
        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas = bytes;
        sendUserMsg.desc = OTAUPDATEREQUEST;
        sendUserMsg.isNeedEncrypt = true;
        return sendUserMsg;
    }


    public static final byte[] getVersionByte = {(byte) 0xab, 0x01, (byte) 0x9c};
    public static final String getVersionStr = "获取版本信息";

    /**
     * 下发查询版本
     */
    public SendMessage getVersion() {
        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas = getVersionByte;
        sendUserMsg.desc = getVersionStr;
        sendUserMsg.isNeedEncrypt = true;
        return sendUserMsg;
    }

//
//    public static final String SendSha256_1="下发的Sha256_1的第一包";
//    //下发sha256第一包
//    public SendMessage getSendSha256_1(byte[] b1) {
//        byte[] bytes = new byte[19];
//        bytes[0] = (byte) 0xdb;//小包这里(byte) 0xdb
//        bytes[1] = (byte) 0x10;
//        bytes[2] = (byte) 0x04;
//
//        for (int i = 3, j = 0; i < bytes.length; i++, j++) {
//            bytes[i] = b1[j];
//        }
//        SendMessage sendUserMsg=new SendMessage();
//        sendUserMsg.datas=bytes;
//        sendUserMsg.desc=SendSha256_1;
//        return sendUserMsg;
//
//    }
    // private static final String SendSha256_2="下发的Sha256_1的第二包";

//    public SendMessage getSendSha256_2(byte[] b1) {
//        byte[] bytes = new byte[19];
//        bytes[0] = (byte) 0xab;
//        bytes[1] = (byte) 0x10;
//        bytes[2] = (byte) 0x95;
//
//        for (int i = 3, j = 0; i < bytes.length; i++, j++) {
//            bytes[i] = b1[j];
//        }
//        SendMessage sendUserMsg=new SendMessage();
//        sendUserMsg.datas=bytes;
//        sendUserMsg.desc=SendSha256_2;
//        return sendUserMsg;
//    }


    private static final String SendUpdateData = "下发更新数据包";

    public SendMessage getSendUpdateData(byte[] bs, int pkgNo) {

        LogUtils.i(TAG, "准备下发更新数据包 bs.length" + bs.length + "---pkgNo=" + pkgNo);

        int noLowHex = pkgNo & 0xff;//低八位
        int noHeightHex = (pkgNo >> 8) & 0xff;//高八位

        byte[] bytes = new byte[20];
        bytes[0] = (byte) 0xad;
        bytes[1] = (byte) 0x12;
        bytes[2] = (byte) noLowHex;
        bytes[3] = (byte) noHeightHex;


        for (int i = 4, j = 0; i < 20; i++, j++) {
            if (j >= bs.length) {
                bytes[i] = (byte) 0xff;
            } else {
                bytes[i] = bs[j];
            }
        }


        //后18位做xor，存入bytes[1]
        byte csXor = 0;
        for (int i = 2; i < bytes.length; i++) {
            csXor ^= bytes[i];
        }
        bytes[1] = csXor;

        //长度低字节再处理
        bytes[2] = (byte) (((pkgNo & 0xff) + 0x90) & 0xff);

        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas = bytes;
        sendUserMsg.desc = SendUpdateData;
        sendUserMsg.isNeedEncrypt = true;
        return sendUserMsg;
    }
}
